from ..extensions import db
from datetime import datetime

class User(db.Model):
    __tablename__ = 'users'
    
    id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), primary_key=True, autoincrement=True, comment='用户ID')
    name = db.Column(db.String(100), nullable=False, comment='姓名')
    phone = db.Column(db.String(20), nullable=False, unique=True, comment='电话')
    email = db.Column(db.String(100), unique=True, comment='邮箱')
    birthday = db.Column(db.Date, comment='生日')
    password = db.Column(db.String(255), nullable=False, comment='密码')
    avatar = db.Column(db.String(255), comment='头像')
    is_disabled = db.Column(db.Boolean, nullable=False, default=False, comment='是否禁用，0=启用，1=禁用')
    house_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), comment='关联房屋ID，逻辑外键 houses.id')
    parking_id = db.Column(db.BigInteger().with_variant(db.Integer, "sqlite"), comment='关联车位ID，逻辑外键 parking_spaces.id')
    created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, comment='创建时间')
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'phone': self.phone,
            'email': self.email,
            'avatar': self.avatar,
            'birthday': str(self.birthday) if self.birthday else None,
            'is_disabled': self.is_disabled,
            'house_id': self.house_id,
            'parking_id': self.parking_id,
            'created_at': str(self.created_at)
        }
